import {getdetail,GoodsBaseInfo,ShopInfo,ParamInfo,getRecommends} from '../../service/detail'
const app = getApp()
Page({
  data: {
    iid:'',
    topImages:[],
    baseInfo: {},
    shopInfo: {},
    detailInfo: {},
    paramInfo: {},
    commentInfo: {},
    recommends: {}

  },
  onLoad: function (options) {
    this.setData({
      iid: options.iid
    })
    //获取各种信息
    this._getdetail()
    //获取推荐数据
    this._getRecommends()
   
  },
  _getdetail() {
    getdetail(this.data.iid).then(e=>{
      const data = e.data.result
      // //取出顶部照片
      const topImages = data.itemInfo.topImages
      //创建baseinfo对象
      const baseInfo = new GoodsBaseInfo(data.itemInfo, data.columns, data.shopInfo.services)
      //创建ShopInfo对象
      const shopInfo = new ShopInfo(data.shopInfo)
      //获取detailInfo信息
      const detailInfo = data.detailInfo
      //创建paramInfo对象
      const paramInfo = new ParamInfo(data.itemParams.info, data.itemParams.rule)
      //获取评论信息
      let commentInfo = {}
      if (data.rate && data.rate.cRate > 0) {
        commentInfo = data.rate.list[0]
      }
      this.setData({
        topImages: topImages,
        baseInfo: baseInfo,
        shopInfo: shopInfo,
        detailInfo: detailInfo,
        paramInfo: paramInfo,
        commentInfo: commentInfo
      })
    })
  },
  _getRecommends(){
    getRecommends().then(e=>{
      this.setData({
        recommends:e.data.data.list
      })
    })
  },
  addcart(){
    const obj = {}
    obj.iid = this.data.iid;
    obj.imageURL = this.data.topImages[0];
    obj.title = this.data.baseInfo.title;
    obj.desc = this.data.baseInfo.desc;
    obj.price = this.data.baseInfo.realPrice;
    

    app.addToCart(obj)
    
    wx.showToast({
      title: '加入购物车成功',
    })
  }
})